# -*- coding:utf8 -*-
from ext import db
# from sqlalchemy import CheckConstraint
from enum import Enum


class TaskStatus(Enum):
    INIT = 'INIT'
    INPROCESS = 'INPROCESS'
    FINISHED = 'FINISHED'
    DISCARD = 'DISCARD'


class TodoTask(db.Model):
    """
    任务列表
    """
    __tablename__ = 'todo_task'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20), nullable=False)
    desc = db.Column(db.Text, nullable=False)
    start_date = db.Column(db.Date, nullable=False)
    start_time = db.Column(db.Time, nullable=False)
    end_date = db.Column(db.Date, nullable=True)
    end_time = db.Column(db.Time, nullable=True)
    assign = db.Column(db.String(4), nullable=False)
    status = db.Column(db.Enum(TaskStatus), nullable=False)

    # 可以通过添加CheckConstraint来实现status字段只能选择INIT、INPROCESS、FINISHED和DISCARD这四个状态
    # __table_args__ = (
    #     CheckConstraint(status.in_(['INIT', 'INPROCESS', 'FINISHED', 'DISCARD']), name='status_check'),
    # )
    def to_json(self):
        return {
            'id': self.id,
            'name': self.name,
            'desc': self.desc,
            'start_date': self.start_date.strftime('%Y-%m-%d'),
            'start_time': self.start_time.strftime('%H:%M:%S'),
            'end_date': self.end_date.strftime('%Y-%m-%d'),
            'end_time': self.end_time.strftime('%H:%M:%S'),
            'assign': self.assign,
            'status': self.status.value
        }
